package com.test.qnews.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.google.gson.Gson;
import com.test.qnews.R;
import com.test.qnews.adapter.DetailsQuestionAdapter;
import com.test.qnews.adapter.QuestionAdapter;
import com.test.qnews.beans.GsonDetailsQuestionBeans;
import com.test.qnews.beans.GsonQuestionBeans;
import com.test.qnews.beans.QuestionBeans;
import com.test.qnews.custom.MyListview;
import com.test.qnews.volley.VolleySingleton;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/21.
 */
public class DetailsQuestionActivity extends BaseActivity implements View.OnClickListener {
    private List<QuestionBeans> datas;
    private DetailsQuestionAdapter adapter;
    private MyListview listView;
    private TextView question_body;
    private boolean flag = true;

    @Override
    protected int setLayout() {
        return R.layout.activity_details_question;
    }

    @Override
    protected void initView() {
        listView = (MyListview) findViewById(R.id.listview_details_question);

        Intent intent = getIntent();
        int pos = intent.getIntExtra("pos", 0);
        Log.d("WWW", "pos:" + pos);
        datas = (List<QuestionBeans>) intent.getSerializableExtra("data");
        String expertId = datas.get(pos).getExpertId();

        findViewById(R.id.iv_lines_max).setOnClickListener(this);
        TextView tv_question_alias = (TextView) findViewById(R.id.tv_question_alias);
        TextView question_name = (TextView) findViewById(R.id.question_name);
        TextView question_title = (TextView) findViewById(R.id.question_title);
        question_body = (TextView) findViewById(R.id.question_body);
        NetworkImageView iv_question_background = (NetworkImageView) findViewById(R.id.iv_question_background);
        NetworkImageView iv_question_head_image = (NetworkImageView) findViewById(R.id.iv_question_head_image);

        tv_question_alias.setText(datas.get(pos).getAlias());
        question_body.setText(datas.get(pos).getDescription());
        question_name.setText(datas.get(pos).getName());
        question_title.setText(datas.get(pos).getTitle());
        VolleySingleton.getInstance().loadImage(iv_question_head_image, datas.get(pos).getHeadpicurl());
        VolleySingleton.getInstance().loadImage(iv_question_background, datas.get(pos).getPicurl());

        data(expertId);
    }


    private void data(String expertId) {
        String url = "http://c.3g.163.com/newstopic/qa/" + expertId + ".html";

        VolleySingleton.getInstance().addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                datas = new ArrayList<>();
                GsonDetailsQuestionBeans gsonBean = gson.fromJson(response, GsonDetailsQuestionBeans.class);

                for (int i = 0; i < gsonBean.getData().getLatestList().size(); i++) {
                    QuestionBeans questionBeans = new QuestionBeans();
                    questionBeans.setDetail_user_content(gsonBean.getData().getLatestList().
                            get(i).getQuestion().getContent());
                    questionBeans.setDetail_user_name(gsonBean.getData().getLatestList().
                            get(i).getQuestion().getUserName());
                    questionBeans.setDetail_user_head_picUrl(gsonBean.getData().getLatestList().
                            get(i).getQuestion().getUserHeadPicUrl());
                    questionBeans.setDetail_specia_list_content(gsonBean.getData().getLatestList().
                            get(i).getAnswer().getContent());
                    questionBeans.setDetail_specia_list_name(gsonBean.getData().getLatestList().
                            get(i).getAnswer().getSpecialistName());
                    questionBeans.setDetail_specia_list_head_picUrl(gsonBean.getData().getLatestList().
                            get(i).getAnswer().getSpecialistHeadPicUrl());

                    Log.d("!!!!!!", gsonBean.getData().getLatestList().get(i).getQuestion().getUserName());
                    datas.add(questionBeans);
                    Log.d("!!!!!!!!!", gsonBean.getData().getLatestList().get(i).getQuestion().getUserHeadPicUrl());
                    adapter = new DetailsQuestionAdapter(datas,DetailsQuestionActivity.this);
                    listView.setAdapter(adapter);

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

    }

    /**
     * 设置TextView的行数
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_lines_max:
                if (flag) {
                    question_body.setSingleLine(false);
                    flag = false;
                }else {
                    question_body.setLines(2);
                    flag = true;
                }
                break;
        }
    }
}